package cn.iocoder.yudao.module.iot.controller.admin.devicerel;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.iot.controller.admin.devicerel.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicerel.DeviceRelDO;
import cn.iocoder.yudao.module.iot.service.devicerel.DeviceRelService;

@Tag(name = "管理后台 - 报警器和气阀绑定")
@RestController
@RequestMapping("/iot/device-rel")
@Validated
public class DeviceRelController {

    @Resource
    private DeviceRelService deviceRelService;

    @PostMapping("/create")
    @Operation(summary = "创建报警器和气阀绑定")
    @PreAuthorize("@ss.hasPermission('iot:device-rel:create')")
    public CommonResult<Integer> createDeviceRel(@Valid @RequestBody DeviceRelSaveReqVO createReqVO) {
        return success(deviceRelService.createDeviceRel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新报警器和气阀绑定")
    @PreAuthorize("@ss.hasPermission('iot:device-rel:update')")
    public CommonResult<Boolean> updateDeviceRel(@Valid @RequestBody DeviceRelSaveReqVO updateReqVO) {
        deviceRelService.updateDeviceRel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除报警器和气阀绑定")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:device-rel:delete')")
    public CommonResult<Boolean> deleteDeviceRel(@RequestParam("id") Integer id) {
        deviceRelService.deleteDeviceRel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得报警器和气阀绑定")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:device-rel:query')")
    public CommonResult<DeviceRelRespVO> getDeviceRel(@RequestParam("id") Integer id) {
        DeviceRelDO deviceRel = deviceRelService.getDeviceRel(id);
        return success(BeanUtils.toBean(deviceRel, DeviceRelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得报警器和气阀绑定分页")
    @PreAuthorize("@ss.hasPermission('iot:device-rel:query')")
    public CommonResult<PageResult<DeviceRelRespVO>> getDeviceRelPage(@Valid DeviceRelPageReqVO pageReqVO) {
        PageResult<DeviceRelDO> pageResult = deviceRelService.getDeviceRelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceRelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出报警器和气阀绑定 Excel")
    @PreAuthorize("@ss.hasPermission('iot:device-rel:export')")
    @OperateLog(type = EXPORT)
    public void exportDeviceRelExcel(@Valid DeviceRelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceRelDO> list = deviceRelService.getDeviceRelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "报警器和气阀绑定.xls", "数据", DeviceRelRespVO.class,
                        BeanUtils.toBean(list, DeviceRelRespVO.class));
    }

}